<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ post.title }} - 个人博客</title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3B82F6',
                        secondary: '#10B981',
                        danger: '#EF4444',
                        dark: '#1F2937',
                        light: '#F9FAFB'
                    },
                    fontFamily: {
                        sans: ['Inter', 'system-ui', 'sans-serif'],
                    },
                }
            }
        }
    </script>
    <style type="text/tailwindcss">
        @layer utilities {
            .content-auto {
                content-visibility: auto;
            }
            .btn {
                @apply px-4 py-2 rounded-md font-medium transition-all duration-200 transform hover:scale-105 focus:outline-none focus:ring-2 focus:ring-opacity-50;
            }
            .btn-primary {
                @apply bg-primary text-white hover:bg-primary/90 focus:ring-primary;
            }
            .btn-danger {
                @apply bg-danger text-white hover:bg-danger/90 focus:ring-danger;
            }
            .btn-secondary {
                @apply bg-secondary text-white hover:bg-secondary/90 focus:ring-secondary;
            }
            .prose {
                @apply text-gray-800 leading-relaxed;
            }
            .prose p {
                @apply mb-4;
            }
            .prose h2 {
                @apply text-2xl font-bold mt-8 mb-4;
            }
            .prose h3 {
                @apply text-xl font-bold mt-6 mb-3;
            }
            .prose ul, .prose ol {
                @apply mb-4 pl-5;
            }
            .prose li {
                @apply mb-1;
            }
            .prose blockquote {
                @apply pl-4 border-l-4 border-gray-300 italic my-4 text-gray-600;
            }
            .prose pre {
                @apply bg-gray-800 text-white p-4 rounded-lg overflow-x-auto mb-4;
            }
            .prose code {
                @apply bg-gray-100 px-1 py-0.5 rounded text-sm;
            }
            .prose pre code {
                @apply bg-transparent p-0 text-white;
            }
            .comment-item {
                @apply border-b border-gray-200 py-4 last:border-0;
            }
        }
    </style>
</head>
<body class="bg-gray-50 min-h-screen font-sans flex flex-col">
    <header class="bg-white shadow-md">
        <div class="container mx-auto px-4 py-4 flex justify-between items-center">
            <h1 class="text-2xl font-bold text-blue-600">个人博客</h1>
            <div>
                {% if current_user.is_authenticated %}
                <div class="flex items-center space-x-4">
                    <a href="{{ url_for('profile') }}" class="flex items-center text-gray-700 hover:text-primary">
                        <img src="{{ current_user.get_avatar(30) }}" alt="{{ current_user.nickname }}" class="w-8 h-8 rounded-full mr-2">
                        <span>{{ current_user.nickname or current_user.email.split('@')[0] }}</span>
                    </a>
                    <a href="{{ url_for('logout') }}" class="bg-red-500 hover:bg-red-700 text-white font-bold py-1 px-3 rounded text-sm">
                        登出
                    </a>
                </div>
                {% else %}
                <div class="space-x-2">
                    <a href="{{ url_for('login') }}" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-1 px-3 rounded text-sm">
                        登录
                    </a>
                    <a href="{{ url_for('register') }}" class="bg-green-500 hover:bg-green-700 text-white font-bold py-1 px-3 rounded text-sm">
                        注册
                    </a>
                </div>
                {% endif %}
            </div>
        </div>
    </header>
    <div class="container mx-auto px-4 py-8 max-w-4xl flex-grow">
        <header class="mb-8">
            <div class="mb-6">
                <a href="{{ url_for('index') }}" class="text-primary hover:underline inline-flex items-center">
                    <i class="fa fa-arrow-left mr-2"></i>返回首页
                </a>
            </div>
            <h1 class="text-3xl md:text-4xl font-bold text-dark mb-4">{{ post.title }}</h1>
            <div class="flex items-center text-gray-500 text-sm mb-6">
                <span class="flex items-center">
                    <i class="fa fa-calendar mr-1"></i>
                    {{ post.created_at.strftime('%Y-%m-%d %H:%M') }}
                </span>
                <span class="mx-2">•</span>
                <span class="flex items-center">
                    <i class="fa fa-clock-o mr-1"></i>
                    最后更新: {{ post.updated_at.strftime('%Y-%m-%d %H:%M') }}
                </span>
                <span class="mx-2">•</span>
                <span class="flex items-center">
                    <i class="fa fa-comments mr-1"></i>
                    {{ post.comments|length }} 条评论
                </span>
            </div>
            <div class="flex space-x-2">
                {% if current_user.is_authenticated and (not post.user_id or post.user_id == current_user.id) %}
                <a href="{{ url_for('edit_post', post_id=post.id) }}" class="btn btn-secondary inline-flex items-center">
                    <i class="fa fa-pencil mr-2"></i>编辑文章
                </a>
                <form action="{{ url_for('delete_post', post_id=post.id) }}" method="POST" class="inline" onsubmit="return confirm('确定要删除这篇文章吗？')">
                    <button type="submit" class="btn btn-danger inline-flex items-center">
                        <i class="fa fa-trash mr-2"></i>删除文章
                    </button>
                </form>
                {% endif %}
            </div>
        </header>

        <article class="bg-white rounded-lg shadow-md p-6 mb-8">
            <div class="prose max-w-none">
                {{ post.content|safe }}
            </div>
        </article>

        <section class="bg-white rounded-lg shadow-md p-6 mb-8">
            <h2 class="text-xl font-bold mb-6 flex items-center">
                <i class="fa fa-comments text-primary mr-2"></i>评论 ({{ post.comments|length }})
            </h2>

            <div class="mb-8">
                <form action="{{ url_for('add_comment', post_id=post.id) }}" method="POST">
                    {% if not current_user.is_authenticated %}
                    <div class="mb-4">
                        <label for="author" class="block text-gray-700 font-medium mb-2">您的名字</label>
                        <input 
                            type="text" 
                            id="author" 
                            name="author" 
                            placeholder="请输入您的名字" 
                            class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary/50 focus:border-primary transition-all"
                            required
                        >
                    </div>
                    {% else %}
                    <div class="mb-4 bg-gray-50 p-3 rounded-lg">
                        <p class="text-gray-700">评论者: <span class="font-medium">{{ current_user.email }}</span></p>
                    </div>
                    {% endif %}
                    <div class="mb-4">
                        <label for="content" class="block text-gray-700 font-medium mb-2">评论内容</label>
                        <textarea 
                            id="content" 
                            name="content" 
                            rows="4" 
                            placeholder="请输入您的评论..." 
                            class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary/50 focus:border-primary transition-all"
                            required
                        ></textarea>
                    </div>
                    <button type="submit" class="btn btn-primary">
                        <i class="fa fa-paper-plane mr-2"></i>提交评论
                    </button>
                </form>
            </div>

            <div class="space-y-0">
                {% if post.comments %}
                    {% for comment in post.comments|sort(attribute='created_at', reverse=true) %}
                    <div class="comment-item">
                        <div class="flex justify-between items-start mb-2">
                            <div class="font-medium">{{ comment.author }}</div>
                            <div class="text-sm text-gray-500">{{ comment.created_at.strftime('%Y-%m-%d %H:%M') }}</div>
                        </div>
                        <div class="text-gray-700">
                            {{ comment.content }}
                        </div>
                    </div>
                    {% endfor %}
                {% else %}
                    <div class="text-center py-8 text-gray-500">
                        <i class="fa fa-comments-o text-4xl mb-3 opacity-30"></i>
                        <p>暂无评论，来发表第一条评论吧！</p>
                    </div>
                {% endif %}
            </div>
        </section>
    </div>

    <footer class="bg-dark text-white py-8 mt-16">
        <div class="container mx-auto px-4 max-w-4xl text-center">
            <p>© {{ now.year }} 个人博客系统</p>
        </div>
    </footer>

    {% with messages = get_flashed_messages(with_categories=true) %}
        {% if messages %}
        <div class="fixed bottom-4 right-4 z-50">
            {% for category, message in messages %}
            <div class="{{ 'bg-green-500' if category == 'success' else 'bg-red-500' }} text-white px-6 py-4 rounded-lg shadow-lg mb-3 flex items-center">
                <i class="fa {{ 'fa-check-circle' if category == 'success' else 'fa-exclamation-circle' }} mr-3"></i>
                <span>{{ message }}</span>
                <button class="ml-auto text-white hover:text-gray-200" onclick="this.parentElement.remove()">
                    <i class="fa fa-times"></i>
                </button>
            </div>
            {% endfor %}
        </div>
        {% endif %}
    {% endwith %}

    <script>
        // 添加当前年份到页脚
        document.addEventListener('DOMContentLoaded', function() {
            const footerYear = document.querySelector('footer p');
            if (footerYear) {
                footerYear.innerHTML = footerYear.innerHTML.replace('{{ now.year }}', new Date().getFullYear());
            }
        });
    </script>
</body>
</html>